package cn.iocoder.yudao.module.call.service.listenaction.action.impl;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONObject;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.mybatis.core.enums.QueryCondition;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.call.api.form.dto.FieldRuleDTO;
import cn.iocoder.yudao.module.call.api.record.CallRecordApi;
import cn.iocoder.yudao.module.call.dal.dataobject.record.CallRecordDO;
import cn.iocoder.yudao.module.call.enums.CallActionEnum;
import cn.iocoder.yudao.module.call.enums.FormFieldTypeEnum;
import cn.iocoder.yudao.module.call.service.listenaction.action.CallAction;
import cn.iocoder.yudao.module.call.service.listenaction.action.dto.ActionInfoDTO;
import cn.iocoder.yudao.module.call.service.listenaction.listen.dto.ListenActionParamDTO;
import cn.iocoder.yudao.module.crm.api.clue.CrmClueApi;
import cn.iocoder.yudao.module.crm.api.clue.dto.CrmClueSaveDTO;
import cn.iocoder.yudao.module.crm.api.follow.CrmFollowRecordApi;
import cn.iocoder.yudao.module.crm.api.module.CrmModuleApi;
import cn.iocoder.yudao.module.crm.api.module.dto.CrmModuleDTO;
import cn.iocoder.yudao.module.crm.enums.common.CrmBizTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class TransClueAction implements CallAction {

    @Resource
    private CrmClueApi crmClueApi;
    @Resource
    private CrmModuleApi crmModuleApi;
    @Resource
    private CallRecordApi callRecordApi;
    @Resource
    private CrmFollowRecordApi crmFollowRecordApi;

    @Override
    public ActionInfoDTO info() {
        CallActionEnum actionEnum = CallActionEnum.ACTION_TRANS_CLUE;
        return new ActionInfoDTO().setTitle(actionEnum.getTitle())
                .setCode(actionEnum.getAction())
                .setRules(rules());
    }

    @Override
    public List<FieldRuleDTO> rules() {
        //模块信息
        List<CrmModuleDTO> moduleList = crmModuleApi.getEnableModuleListByTenant(TenantContextHolder.getTenantId());
        Map<Object, Object> moduleMap = CollectionUtils.convertMap(moduleList, CrmModuleDTO::getId, CrmModuleDTO::getName);
        //转入位置
        Map<Object, Object> posMap = MapUtil.builder().put(1, "线索列表").put(CommonStatusEnum.ENABLE.getStatus(), "线索公海").build();
        return ListUtil.of(
                new FieldRuleDTO().setField("transPos").setDesc("转入位置").setType(FormFieldTypeEnum.SELECT.getType()).setOptions(posMap).setCondition(QueryCondition.EQ),
                //模块
                new FieldRuleDTO().setField("moduleId").setDesc("模块").setType(FormFieldTypeEnum.SELECT.getType()).setOptions(moduleMap).setCondition(QueryCondition.EQ)
        );
    }

    @Override
    public void execute(ListenActionParamDTO paramDTO) {
        //有clueId的表示从线索导入机器人呼叫任务的,如果存在就则不需要再插入
        CrmClueSaveDTO saveDTO = new CrmClueSaveDTO();
        CallRecordDO record = paramDTO.getCallRecordDO();
        JSONObject actionParams = parse(paramDTO.getListenActionDO().getActionParams());
        Long ownerUserId = null;
        boolean isPool = actionParams.getInt("transPos") == 2; //1线索列表 2线索公海
        Long moduleId = actionParams.getLong("moduleId");
        if (!isPool) {
            ownerUserId = crmClueApi.getRobotDistributeUser(moduleId);
        }
        saveDTO.setMobile(paramDTO.getCallRecordDO().getCallee());
        saveDTO.setName("来自AI外呼的线索");
        saveDTO.setOwnerUserId(ownerUserId); //根据分配规则进行分配
        saveDTO.setReqNo(record.getReqNo()); //
        saveDTO.setModuleId(moduleId);
        saveDTO.setLevelId(paramDTO.getIvrIntentionLabelStatisticsDO().getLevel());
        Long clueId = crmClueApi.createClue(saveDTO);
        callRecordApi.updateCallRecordClueInfo(record.getId(), clueId);
        //跟进记录
        crmFollowRecordApi.addCrmRobotCallFollowRecord(CrmBizTypeEnum.CRM_CLUE, ownerUserId, clueId, record.getId(), true, record.getRecordUrl());
    }


}
